package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/*
思路：首先判满，如果书架已满，则给出提示信息后退出
     让用户输入想要加入的图书信息，并根据这些信息创建对象
     将这个对象加入到书架（图书对象数组）的末尾，然后把存储图书的数目+1
 */
public class AddOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        //首先判满
        if(bookList.getUsedSize() == bookList.getBooks().length) {
            System.out.println("书架已满，无法新增图书信息！");
            return;
        }
        //输入要添加的图书信息

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书的书名:");
        String name = sc.nextLine();
        System.out.println("请输入图书的作者:");
        String author = sc.nextLine();
        System.out.println("请输入图书的价格:");
        String inputPrice = sc.nextLine();
        double price = Double.parseDouble(inputPrice);
        System.out.println("请输入图书的类型:");
        String type = sc.nextLine();

       //根据输入的信息构造对象，然后添加到顺序表中
        Book newBook = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();
        bookList.getBooks()[currentSize] = newBook;
        bookList.setUsedSize(currentSize + 1);
        System.out.println("添加成功");
    }
}
